﻿Imports System.ComponentModel.DataAnnotations
Imports Services
Imports Models.Solution

Namespace MapServices

    Public Class LayerLibraries
        Inherits BaseService

        Sub New(token As String)
            Me.token = token
        End Sub

        Sub New(request As System.Net.Http.HttpRequestMessage)
            Me.loadToken(request)
        End Sub

        ''' <summary>
        ''' Gets the specified id.
        ''' </summary>
        ''' <param name="id">The id.</param>
        ''' <returns></returns>
        Public Function [Get](id As Integer) As LayerLibraryVO
            Try
                'Me.checkAccess("FP_GETLAYERS")
                Dim oEntity As LayerLibraryVO = Nothing

                'Using oDB As New FastpathData.BaseModel.fastpathContainer("name=FastPathModelContainer")
                '    Dim oDataEntity As FastpathData.BaseModel.LayerLibrary = (From u In oDB.LayerLibraries Where u.IdLayerLibrary = id Select u).FirstOrDefault()

                '    If oDataEntity IsNot Nothing Then
                '        oEntity = New LayerLibraryVO(oDataEntity)
                '    End If
                'End Using

                Return oEntity

            Catch ex As Exception
                MainLib.Helpers.ExceptionWrapper(ex)
            End Try
        End Function



        ''' <summary>
        ''' Gets the content of LayerLibrary with specified id.
        ''' </summary>
        ''' <param name="id">The id.</param>
        ''' <returns></returns>
        Public Function GetContent(id As Integer) As String
            Try
                'Using oDB As New FastpathData.BaseModel.fastpathContainer("name=FastPathModelContainer")
                '    Dim oDataEntity As FastpathData.BaseModel.LayerLibrary = (From u In oDB.LayerLibraries Where u.IdLayerLibrary = id Select u).FirstOrDefault()

                '    If oDataEntity IsNot Nothing Then
                '        Return oDataEntity.Content
                '    End If
                'End Using
            Catch ex As Exception
                MainLib.Helpers.ExceptionWrapper(ex)
            End Try
            Return ""
        End Function



        ''' <summary>
        ''' Get List of Status
        ''' </summary>
        ''' <returns>Array of LayersLibrary</returns>
        ''' <remarks></remarks>
        Public Function List() As IEnumerable(Of LayerLibraryVO)
            Try
                'Me.checkAccess("FP_GETLAYERS")
                Dim oResult As New Generic.List(Of LayerLibraryVO)

                Dim idCompany As Integer = 1
                If Not Me.ExistSession() Then
                    Return Nothing
                End If

                idCompany = Me.getCurrentSession().companyId
                'Using oDB As New FastpathData.BaseModel.fastpathContainer("name=FastPathModelContainer")
                '    Dim oList As Generic.List(Of FastpathData.BaseModel.LayerLibrary)
                '    oList = oDB.LayerLibraries.Where(Function(l) l.IdCompany = idCompany).ToList()

                '    For Each entity As FastpathData.BaseModel.LayerLibrary In oList
                '        Dim vo As New LayerLibraryVO(entity)
                '        ' clean content for list
                '        vo.content = ""
                '        oResult.Add(vo)
                '    Next
                'End Using

                Return oResult

            Catch ex As Exception
                MainLib.Helpers.ExceptionWrapper(ex)
            End Try
        End Function



        ''' <summary>
        ''' Create a Layerlibrary
        ''' </summary>
        ''' <param name="layerLibrary"></param>
        ''' <returns>The new ID  of the entity</returns>
        ''' <remarks></remarks>
        Public Function Create(layerLibrary As LayerLibraryVO) As Integer
            Try
                'Me.checkAccess("FP_CREATELAYER")
                Dim nRes As Integer = -1

                If Not Me.ExistSession Then
                    Throw New Exception("You must login")
                End If


                'Using oDB As New FastpathData.BaseModel.fastpathContainer("name=FastPathModelContainer")

                '    Dim oDataEntity As New FastpathData.BaseModel.LayerLibrary()
                '    If oDataEntity IsNot Nothing Then
                '        oDataEntity.IdUser = Me.getCurrentSession().loginId
                '        oDataEntity.IdCompany = Me.getCurrentSession().companyId
                '        oDataEntity.Name = layerLibrary.name
                '        oDataEntity.Content = System.Web.HttpContext.Current.Server.UrlDecode(layerLibrary.content)
                '        oDataEntity.CreatedDate = Now
                '        oDB.LayerLibraries.AddObject(oDataEntity)
                '    End If

                '    If oDB.SaveChanges() > 0 Then
                '        nRes = oDataEntity.IdLayerLibrary
                '    End If
                'End Using

                Return nRes

            Catch ex As Exception
                MainLib.Helpers.ExceptionWrapper(ex)
                Return Nothing
            End Try

        End Function

        ''' <summary>
        ''' Delete a LayerLibrary
        ''' </summary>
        ''' <param name="id">IdLayerLibrary</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Delete(id As Integer) As Integer
            Try
                'Me.checkAccess("FP_DELETELAYER")
                Dim nRes As Integer = -1

                'Using oDB As New FastpathData.BaseModel.fastpathContainer("name=FastPathModelContainer")

                '    Dim oDataEntity As FastpathData.BaseModel.LayerLibrary = (From c In oDB.LayerLibraries Where c.IdLayerLibrary = id Select c).FirstOrDefault()
                '    If oDataEntity.IdCompany = Me.getCurrentSession().companyId Then
                '        oDB.DeleteObject(oDataEntity)
                '        nRes = oDB.SaveChanges()
                '    End If
                'End Using

                Return nRes

            Catch ex As Exception
                MainLib.Helpers.ExceptionWrapper(ex)
                Return Nothing
            End Try

        End Function

    End Class

End Namespace